<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 日期时间</h1>
        <p>
            java.util 包提供了 Date 类来封装当前的日期和时间。

            Date 类提供两个构造函数来实例化 Date 对象。</p><p>
        第一个构造函数使用当前日期和时间来初始化对象。</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>
            第二个构造函数接收一个参数，该参数是从1970年1月1日起的毫秒数。
        </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-types">long</span><span class="hl-code"> </span><span class="hl-identifier">millisec</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>
            Date对象创建以后，可以调用下面的方法。</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:38px;">
                    序号</th>
                <th style="width:538px;">
                    方法和描述</th>
            </tr>
            <tr>
                <td style="width:38px;">
                    1</td>
                <td style="width:538px;">
                    <strong>boolean after(Date date)</strong><br>
                    若当调用此方法的Date对象在指定日期之后返回true,否则返回false。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    2</td>
                <td style="width:538px;">
                    <strong>boolean before(Date date)</strong><br>
                    若当调用此方法的Date对象在指定日期之前返回true,否则返回false。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    3</td>
                <td style="width:538px;">
                    <strong>Object clone( )</strong><br>
                    返回此对象的副本。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    4</td>
                <td style="width:538px;">
                    <strong>int compareTo(Date date)</strong><br>
                    比较当调用此方法的Date对象和指定日期。两者相等时候返回0。调用对象在指定日期之前则返回负数。调用对象在指定日期之后则返回正数。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    5</td>
                <td style="width:538px;">
                    <strong>int compareTo(Object obj)</strong><br>
                    若obj是Date类型则操作等同于compareTo(Date) 。否则它抛出ClassCastException。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    6</td>
                <td style="width:538px;">
                    <strong>boolean equals(Object date)</strong><br>
                    当调用此方法的Date对象和指定日期相等时候返回true,否则返回false。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    7</td>
                <td style="width:538px;">
                    <strong>long getTime( )</strong><br>
                    返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    8</td>
                <td style="width:538px;">
                    <strong>int hashCode( )</strong><br>
                    &nbsp;返回此对象的哈希码值。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    9</td>
                <td style="width:538px;">
                    <strong>void setTime(long time)</strong><br>
                    &nbsp;<br>
                    用自1970年1月1日00:00:00 GMT以后time毫秒数设置时间和日期。</td>
            </tr>
            <tr>
                <td style="width:38px;">
                    10</td>
                <td style="width:538px;">
                    <strong>String toString( )</strong><br>
                    把此 Date 对象转换为以下形式的 String： dow mon dd hh:mm:ss zzz yyyy 其中： dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。</td>
            </tr>
            </tbody>
        </table>
        <hr>
        <h2>获取当前日期时间</h2>
        <p>
            Java中获取当前日期和时间很简单，使用 Date 对象的 toString() 方法来打印当前日期和时间，如下所示：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Date</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-comment">//</span><span class="hl-comment"> 初始化 Date 对象</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

       </span><span class="hl-comment">//</span><span class="hl-comment"> 使用 toString() 函数显示日期时间</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">date</span><span class="hl-code">.</span><span class="hl-identifier">toString</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>

        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Mon</span><span class="pln"> </span><span class="typ">May</span><span class="pln"> </span><span class="lit">04</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">51</span><span class="pun">:</span><span class="lit">52</span><span class="pln"> CDT </span><span class="lit">2013</span></pre><hr>
        <h2>日期比较</h2>
        <p>Java使用以下三种方法来比较两个日期：</p>
        <ul>
            <li>
                使用 getTime() 方法获取两个日期（自1970年1月1日经历的毫秒数值），然后比较这两个值。</li>
            <li>
                使用方法 before()，after() 和 equals()。例如，一个月的12号比18号早，则 new Date(99, 2, 12).before(new Date (99, 2, 18)) 返回true。</li>
            <li>
                使用 compareTo() 方法，它是由 Comparable 接口定义的，Date 类实现了这个接口。</li>
        </ul>
        <hr>
        <h2>使用 SimpleDateFormat 格式化日期</h2>
        <p>SimpleDateFormat 是一个以语言环境敏感的方式来格式化和分析日期的类。SimpleDateFormat 允许你选择任何用户自定义日期时间格式来运行。例如：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

      </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">dNow</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">SimpleDateFormat</span><span class="hl-code"> </span><span class="hl-identifier">ft</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">SimpleDateFormat</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">E yyyy.MM.dd 'at' hh:mm:ss a zzz</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Current Date: </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">ft</span><span class="hl-code">.</span><span class="hl-identifier">format</span><span class="hl-brackets">(</span><span class="hl-identifier">dNow</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">SimpleDateFormat</span><span class="pln"> ft </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">SimpleDateFormat</span><span class="pln"> </span><span class="pun">(</span><span class="str">"E yyyy.MM.dd 'at' hh:mm:ss a zzz"</span><span class="pun">);</span></pre><p>
        这一行代码确立了转换的格式，其中 yyyy 是完整的公元年，MM 是月份，dd 是日期，HH:mm:ss 是时、分、秒。
    </p><p>
        <strong>注意</strong>:有的格式大写，有的格式小写，例如 MM 是月份，mm 是分；HH 是 24 小时制，而 hh 是 12 小时制。</p>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Current</span><span class="pln"> </span><span class="typ">Date</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Wed</span><span class="pln"> </span><span class="lit">2016.11</span><span class="pun">.</span><span class="lit">09</span><span class="pln"> at </span><span class="lit">08</span><span class="pun">:</span><span class="lit">23</span><span class="pun">:</span><span class="lit">19</span><span class="pln"> AM UTC</span></pre>
        <hr>
        <h2>日期和时间的格式化编码</h2>
        <p>时间模式字符串用来指定时间格式。在此模式中，所有的 ASCII 字母被保留为模式字母，定义如下：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:66px;">
                    <strong>字母</strong></th>
                <th style="width:151px;">
                    <strong>描述</strong></th>
                <th style="width:128px;">
                    <strong>示例</strong></th>
            </tr>
            <tr>
                <td style="width:66px;">
                    G</td>
                <td style="width:151px;">
                    纪元标记</td>
                <td style="width:128px;">
                    AD</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    y</td>
                <td style="width:151px;">
                    四位年份</td>
                <td style="width:128px;">
                    2001</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    M</td>
                <td style="width:151px;">
                    月份</td>
                <td style="width:128px;">
                    July or 07</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    d</td>
                <td style="width:151px;">
                    一个月的日期</td>
                <td style="width:128px;">
                    10</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    h</td>
                <td style="width:151px;">
                    &nbsp;A.M./P.M. (1~12)格式小时</td>
                <td style="width:128px;">
                    12</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    H</td>
                <td style="width:151px;">
                    一天中的小时 (0~23)</td>
                <td style="width:128px;">
                    22</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    m</td>
                <td style="width:151px;">
                    分钟数</td>
                <td style="width:128px;">
                    30</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    s</td>
                <td style="width:151px;">
                    秒数</td>
                <td style="width:128px;">
                    55</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    S</td>
                <td style="width:151px;">
                    毫秒数</td>
                <td style="width:128px;">
                    234</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    E</td>
                <td style="width:151px;">
                    星期几</td>
                <td style="width:128px;">
                    Tuesday</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    D</td>
                <td style="width:151px;">
                    一年中的日子</td>
                <td style="width:128px;">
                    360</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    F</td>
                <td style="width:151px;">
                    一个月中第几周的周几</td>
                <td style="width:128px;">
                    2 (second Wed. in July)</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    w</td>
                <td style="width:151px;">
                    一年中第几周</td>
                <td style="width:128px;">
                    40</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    W</td>
                <td style="width:151px;">
                    一个月中第几周</td>
                <td style="width:128px;">
                    1</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    a</td>
                <td style="width:151px;">
                    A.M./P.M. 标记</td>
                <td style="width:128px;">
                    PM</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    k</td>
                <td style="width:151px;">
                    一天中的小时(1~24)</td>
                <td style="width:128px;">
                    24</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    K</td>
                <td style="width:151px;">
                    &nbsp;A.M./P.M. (0~11)格式小时</td>
                <td style="width:128px;">
                    10</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    z</td>
                <td style="width:151px;">
                    时区</td>
                <td style="width:128px;">
                    Eastern Standard Time</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    '</td>
                <td style="width:151px;">
                    文字定界符</td>
                <td style="width:128px;">
                    Delimiter</td>
            </tr>
            <tr>
                <td style="width:66px;">
                    "</td>
                <td style="width:151px;">
                    单引号</td>
                <td style="width:128px;">
                    `</td>
            </tr>
            </tbody>
        </table>
        <hr>
        <h2>使用printf格式化日期</h2>
        <p>printf 方法可以很轻松地格式化时间和日期。使用两个字母格式，它以 <span class="marked">%t</span> 开头并且以下面表格中的一个字母结尾。</p>

        <table class="reference">
            <tbody>
            <tr>
                <th>
                    <p>转&nbsp; 换&nbsp; 符</p>
                </th>
                <th>
                    <p>说&nbsp;&nbsp;&nbsp; 明</p>
                </th>
                <th>
                    <p>示&nbsp;&nbsp;&nbsp; 例</p>
                </th>
            </tr>
            <tr>
                <td>
                    <p>c</p>
                </td>
                <td>
                    <p>包括全部日期和时间信息</p>
                </td>
                <td>
                    <p>星期六 十月 27 14:21:20 CST 2007</p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>F</p>
                </td>
                <td>
                    <p>"年-月-日"格式</p>
                </td>
                <td>
                    <p>2007-10-27</p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>D</p>
                </td>
                <td>
                    <p>"月/日/年"格式</p>
                </td>
                <td>
                    <p>10/27/07</p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>r</p>
                </td>
                <td>
                    <p>"HH:MM:SS PM"格式（12时制）</p>
                </td>
                <td>
                    <p>02:25:51 下午</p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>T</p>
                </td>
                <td>
                    <p>"HH:MM:SS"格式（24时制）</p>
                </td>
                <td>
                    <p>14:28:16</p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>R</p>
                </td>
                <td>
                    <p>"HH:MM"格式（24时制）</p>
                </td>
                <td>
                    <p>14:28</p>
                </td>
            </tr>
            </tbody>
        </table>
        <p>更多 <strong>printf</strong> 解析可以参见：<a href="/w3cnote/java-printf-formate-demo.html" target="_blank">Java 格式化输出 printf 例子</a>
        </p>
        <h3>
            实例</h3>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Date</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
     </span><span class="hl-comment">//</span><span class="hl-comment"> 初始化 Date 对象</span><span class="hl-comment"></span><span class="hl-code">
     </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

     </span><span class="hl-comment">//</span><span class="hl-comment">c的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">全部日期和时间信息：%tc%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">f的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">年-月-日格式：%tF%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">d的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">月/日/年格式：%tD%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">r的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">HH:MM:SS PM格式（12时制）：%tr%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">t的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">HH:MM:SS格式（24时制）：%tT%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">R的使用  </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">HH:MM格式（24时制）：%tR</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">全部日期和时间信息：星期一</span><span class="pln"> </span><span class="pun">九月</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">36</span><span class="pln"> CST </span><span class="lit">2012</span><span class="pln">
</span><span class="pun">年-月-日格式：</span><span class="lit">2012</span><span class="pun">-</span><span class="lit">09</span><span class="pun">-</span><span class="lit">10</span><span class="pln">
</span><span class="pun">月/日/年格式：</span><span class="lit">09</span><span class="pun">/</span><span class="lit">10</span><span class="pun">/</span><span class="lit">12</span><span class="pln">
HH</span><span class="pun">:</span><span class="pln">MM</span><span class="pun">:</span><span class="pln">SS PM</span><span class="pun">格式（</span><span class="lit">12</span><span class="pun">时制）：</span><span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">36</span><span class="pln"> </span><span class="pun">上午</span><span class="pln">
HH</span><span class="pun">:</span><span class="pln">MM</span><span class="pun">:</span><span class="pln">SS</span><span class="pun">格式（</span><span class="lit">24</span><span class="pun">时制）：</span><span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">36</span><span class="pln">
HH</span><span class="pun">:</span><span class="pln">MM</span><span class="pun">格式（</span><span class="lit">24</span><span class="pun">时制）：</span><span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pln">  </span></pre>
        <p>如果你需要重复提供日期，那么利用这种方式来格式化它的每一部分就有点复杂了。因此，可以利用一个格式化字符串指出要被格式化的参数的索引。</p>
        <p>索引必须紧跟在%后面，而且必须以$结束。例如：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Date</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-comment">//</span><span class="hl-comment"> 初始化 Date 对象</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

       </span><span class="hl-comment">//</span><span class="hl-comment"> 使用toString()显示日期和时间</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">%1$s %2$tB %2$td, %2$tY</span><span class="hl-quotes">"</span><span class="hl-code">,
                         </span><span class="hl-quotes">"</span><span class="hl-string">Due date:</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Due</span><span class="pln"> date</span><span class="pun">:</span><span class="pln"> </span><span class="typ">February</span><span class="pln"> </span><span class="lit">09</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2014</span></pre>
        <p>或者，你可以使用 &lt; 标志。它表明先前被格式化的参数要被再次使用。例如：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Date</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-comment">//</span><span class="hl-comment"> 初始化 Date 对象</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

       </span><span class="hl-comment">//</span><span class="hl-comment"> 显示格式化时间</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">%s %tB %&lt;te, %&lt;tY</span><span class="hl-quotes">"</span><span class="hl-code">,
                         </span><span class="hl-quotes">"</span><span class="hl-string">Due date:</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Due</span><span class="pln"> date</span><span class="pun">:</span><span class="pln"> </span><span class="typ">February</span><span class="pln"> </span><span class="lit">09</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2014</span></pre>
        <p>定义日期格式的转换符可以使日期通过指定的转换符生成新字符串。这些日期转换符如下所示：</p>

        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">b的使用，月份简称  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code">=</span><span class="hl-identifier">String</span><span class="hl-code">.</span><span class="hl-identifier">format</span><span class="hl-brackets">(</span><span class="hl-identifier">Locale</span><span class="hl-code">.</span><span class="hl-identifier">US</span><span class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">英文月份简称：%tb</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">本地月份简称：%tb%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">B的使用，月份全称  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">str</span><span class="hl-code">=</span><span class="hl-identifier">String</span><span class="hl-code">.</span><span class="hl-identifier">format</span><span class="hl-brackets">(</span><span class="hl-identifier">Locale</span><span class="hl-code">.</span><span class="hl-identifier">US</span><span class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">英文月份全称：%tB</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">本地月份全称：%tB%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">a的使用，星期简称  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">str</span><span class="hl-code">=</span><span class="hl-identifier">String</span><span class="hl-code">.</span><span class="hl-identifier">format</span><span class="hl-brackets">(</span><span class="hl-identifier">Locale</span><span class="hl-code">.</span><span class="hl-identifier">US</span><span class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">英文星期的简称：%ta</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">A的使用，星期全称  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">本地星期的简称：%tA%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">C的使用，年前两位  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">年的前两位数字（不足两位前面补0）：%tC%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">y的使用，年后两位  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">年的后两位数字（不足两位前面补0）：%ty%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">j的使用，一年的天数  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">一年中的天数（即年的第几天）：%tj%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">m的使用，月份  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">两位数字的月份（不足两位前面补0）：%tm%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">d的使用，日（二位，不够补零）  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">两位数字的日（不足两位前面补0）：%td%n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment">e的使用，日（一位不补零）  </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">月份的日（前面不补0）：%te</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">date</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>

        </div>
        <p>输出结果为：</p><pre class="prettyprint prettyprinted" style=""><span class="pun">英文月份简称：</span><span class="typ">May</span><span class="pln">
</span><span class="pun">本地月份简称：五月</span><span class="pln">
</span><span class="pun">英文月份全称：</span><span class="typ">May</span><span class="pln">
</span><span class="pun">本地月份全称：五月</span><span class="pln">
</span><span class="pun">英文星期的简称：</span><span class="typ">Thu</span><span class="pln">
</span><span class="pun">本地星期的简称：星期四</span><span class="pln">
</span><span class="pun">年的前两位数字（不足两位前面补</span><span class="lit">0</span><span class="pun">）：</span><span class="lit">20</span><span class="pln">
</span><span class="pun">年的后两位数字（不足两位前面补</span><span class="lit">0</span><span class="pun">）：</span><span class="lit">17</span><span class="pln">
</span><span class="pun">一年中的天数（即年的第几天）：</span><span class="lit">124</span><span class="pln">
</span><span class="pun">两位数字的月份（不足两位前面补</span><span class="lit">0</span><span class="pun">）：</span><span class="lit">05</span><span class="pln">
</span><span class="pun">两位数字的日（不足两位前面补</span><span class="lit">0</span><span class="pun">）：</span><span class="lit">04</span><span class="pln">
</span><span class="pun">月份的日（前面不补</span><span class="lit">0</span><span class="pun">）：</span><span class="lit">4</span></pre>
        <hr>
        <h2>解析字符串为时间</h2>
        <p>
            SimpleDateFormat 类有一些附加的方法，特别是parse()，它试图按照给定的SimpleDateFormat 对象的格式化存储来解析字符串。例如：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DateDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">SimpleDateFormat</span><span class="hl-code"> </span><span class="hl-identifier">ft</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">SimpleDateFormat</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">yyyy-MM-dd</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">input</span><span class="hl-code"> = </span><span class="hl-identifier">args</span><span class="hl-code">.</span><span class="hl-identifier">length</span><span class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code"> ? </span><span class="hl-quotes">"</span><span class="hl-string">1818-11-11</span><span class="hl-quotes">"</span><span class="hl-code"> : </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">;

      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">input</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> Parses as </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">Date</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-code">;

      </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">t</span><span class="hl-code"> = </span><span class="hl-identifier">ft</span><span class="hl-code">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">input</span><span class="hl-brackets">)</span><span class="hl-code">;
          </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">t</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">ParseException</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Unparseable using </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">ft</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>

        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ java </span><span class="typ">DateDemo</span><span class="pln">
</span><span class="lit">1818</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">11</span><span class="pln"> </span><span class="typ">Parses</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> </span><span class="typ">Wed</span><span class="pln"> </span><span class="typ">Nov</span><span class="pln"> </span><span class="lit">11</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> GMT </span><span class="lit">1818</span><span class="pln">
$ java </span><span class="typ">DateDemo</span><span class="pln"> </span><span class="lit">2007</span><span class="pun">-</span><span class="lit">12</span><span class="pun">-</span><span class="lit">01</span><span class="pln">
</span><span class="lit">2007</span><span class="pun">-</span><span class="lit">12</span><span class="pun">-</span><span class="lit">01</span><span class="pln"> </span><span class="typ">Parses</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> </span><span class="typ">Sat</span><span class="pln"> </span><span class="typ">Dec</span><span class="pln"> </span><span class="lit">01</span><span class="pln"> </span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> GMT </span><span class="lit">2007</span></pre>
        <hr><h2>Java 休眠(sleep)</h2>
        <p>sleep()使当前线程进入停滞状态（阻塞当前线程），让出CPU的使用、目的是不让当前线程独自霸占该进程所获的CPU资源，以留一定时间给其他线程执行的机会。</p>
        <p>你可以让程序休眠一毫秒的时间或者到您的计算机的寿命长的任意段时间。例如，下面的程序会休眠3秒：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">SleepDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">Thread</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-number">1000</span><span class="hl-code">*</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 休眠3秒</span><span class="hl-comment"></span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Got an exception!</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">17</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">30</span><span class="pln"> CST </span><span class="lit">2015</span><span class="pln">

</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">17</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">33</span><span class="pln"> CST </span><span class="lit">2015</span></pre>
        <hr>
        <h2>测量时间</h2>
        <p>下面的一个例子表明如何测量时间间隔（以毫秒为单位）：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">DiffDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-types">long</span><span class="hl-code"> </span><span class="hl-identifier">start</span><span class="hl-code"> = </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">currentTimeMillis</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">Thread</span><span class="hl-code">.</span><span class="hl-identifier">sleep</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-code">*</span><span class="hl-number">60</span><span class="hl-code">*</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Date</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-types">long</span><span class="hl-code"> </span><span class="hl-identifier">end</span><span class="hl-code"> = </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">currentTimeMillis</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-types">long</span><span class="hl-code"> </span><span class="hl-identifier">diff</span><span class="hl-code"> = </span><span class="hl-identifier">end</span><span class="hl-code"> - </span><span class="hl-identifier">start</span><span class="hl-code">;
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Difference is : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">diff</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Got an exception!</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">08</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> CST </span><span class="lit">2016</span><span class="pln">

</span><span class="typ">Fri</span><span class="pln"> </span><span class="typ">Jan</span><span class="pln"> </span><span class="lit">08</span><span class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span class="lit">48</span><span class="pun">:</span><span class="lit">50</span><span class="pln"> CST </span><span class="lit">2016</span><span class="pln">

</span><span class="typ">Difference</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">3019</span></pre>
        <hr>
        <h2>Calendar类</h2>
        <p>我们现在已经能够格式化并创建一个日期对象了，但是我们如何才能设置和获取日期数据的特定部分呢，比如说小时，日，或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用Calendar 类。 </p>

        <p>Calendar类的功能要比Date类强大很多，而且在实现方式上也比Date类要复杂一些。</p>
        <p>Calendar类是一个抽象类，在实际使用时实现特定的子类的对象，创建对象的过程对程序员来说是透明的，只需要使用getInstance方法创建即可。</p>
        <h3>创建一个代表系统当前日期的Calendar对象</h3>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Calendar</span><span class="pln"> c </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">();</span><span class="com">//默认是当前日期</span></pre>
        <h3>创建一个指定日期的Calendar对象</h3>
        <p>使用Calendar类代表特定的时间，需要首先创建一个Calendar的对象，然后再设定该对象中的年月日参数来完成。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">//创建一个代表2009年6月12日的Calendar对象</span><span class="pln">
</span><span class="typ">Calendar</span><span class="pln"> c1 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">();</span><span class="pln">
c1</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="lit">2009</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">12</span><span class="pun">);</span></pre>
        <h3>Calendar类对象字段类型</h3>
        <p>Calendar类中用以下这些常量表示不同的意义，jdk内的很多类其实都是采用的这种思想</p>
        <table class="reference">
            <tbody>
            <tr><th>常量</th><th>描述</th></tr>
            <tr><td>Calendar.YEAR</td><td>年份</td></tr>
            <tr><td>Calendar.MONTH</td><td>月份</td></tr>
            <tr><td>Calendar.DATE</td><td>日期</td></tr>
            <tr><td>Calendar.DAY_OF_MONTH</td><td>日期，和上面的字段意义完全相同</td></tr>
            <tr><td>Calendar.HOUR</td><td>12小时制的小时</td></tr>
            <tr><td>Calendar.HOUR_OF_DAY</td><td>24小时制的小时</td></tr>
            <tr><td>Calendar.MINUTE</td><td>分钟</td></tr>
            <tr><td>Calendar.SECOND</td><td>秒</td></tr>
            <tr><td>Calendar.DAY_OF_WEEK</td><td>星期几</td>
            </tr>
            </tbody>
        </table>
        <h3>Calendar类对象信息的设置</h3>
        <p><strong>Set设置</strong></p>
        <p>如：</p><pre class="prettyprint prettyprinted" style=""><span class="typ">Calendar</span><span class="pln"> c1 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">();</span></pre>
        <p>调用：</p><pre class="prettyprint prettyprinted" style=""><span class="kwd">public</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> year</span><span class="pun">,</span><span class="kwd">int</span><span class="pln"> month</span><span class="pun">,</span><span class="kwd">int</span><span class="pln"> date</span><span class="pun">)</span></pre><pre class="prettyprint prettyprinted" style=""><span class="pln">c1</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="lit">2009</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">12</span><span class="pun">);</span><span class="com">//把Calendar对象c1的年月日分别设这为：2009、6、12</span></pre>
        <p>利用字段类型设置</p>
        <p>如果只设定某个字段，例如日期的值，则可以使用如下set方法：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> field</span><span class="pun">,</span><span class="kwd">int</span><span class="pln"> value</span><span class="pun">)</span></pre>

        <p>把 c1对象代表的日期设置为10号，其它所有的数值会被重新计算</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">c1</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">DATE</span><span class="pun">,</span><span class="lit">10</span><span class="pun">);</span></pre>
        <p>把c1对象代表的年份设置为2008年，其他的所有数值会被重新计算</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">c1</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">YEAR</span><span class="pun">,</span><span class="lit">2008</span><span class="pun">);</span></pre>
        <p>其他字段属性set的意义以此类推</p>
        <p><strong>Add设置</strong></p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Calendar</span><span class="pln"> c1 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">();</span></pre>

        <p>把c1对象的日期加上10，也就是c1也就表示为10天后的日期，其它所有的数值会被重新计算</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">c1</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">DATE</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">);</span></pre>

        <p>把c1对象的日期减去10，也就是c1也就表示为10天前的日期，其它所有的数值会被重新计算</p>

        <pre class="prettyprint prettyprinted" style=""><span class="pln">c1</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="typ">Calendar</span><span class="pun">.</span><span class="pln">DATE</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span class="lit">10</span><span class="pun">);</span></pre>
        <p>其他字段属性的add的意义以此类推</p>
        <h3>Calendar类对象信息的获得</h3>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">Calendar</span><span class="hl-code"> </span><span class="hl-identifier">c1</span><span class="hl-code"> = </span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">getInstance</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得年份</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">year</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">YEAR</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得月份</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">month</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">MONTH</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得日期</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">date</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">DATE</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得小时</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">hour</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">HOUR_OF_DAY</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得分钟</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">minute</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">MINUTE</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得秒</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">second</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">SECOND</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 获得星期几（注意（这个与Date类是不同的）：1代表星期日、2代表星期1、3代表星期二，以此类推）</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">day</span><span class="hl-code"> = </span><span class="hl-identifier">c1</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">DAY_OF_WEEK</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre>
            </div>
        </div>
        <hr>

        <h2>GregorianCalendar类</h2>
        <p>Calendar类实现了公历日历，GregorianCalendar是Calendar类的一个具体实现。
        </p><p>Calendar 的getInstance（）方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段：AD和BC。这是代表公历定义的两个时代。
    </p><p>下面列出GregorianCalendar对象的几个构造方法：</p>
        <table class="reference">
            <tbody>
            <tr>
                <td style="width:47px;">
                    <strong>序号</strong></td>
                <td style="width:529px;">
                    <strong>构造函数和说明</strong></td>
            </tr>
            <tr>
                <td style="width:47px;">
                    1</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar() </strong><br>
                    在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    2</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(int year, int month, int date) </strong><br>
                    在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    3</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(int year, int month, int date, int hour, int minute) </strong><br>
                    为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    4</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(int year, int month, int date, int hour, int minute, int second) </strong><br>
                    &nbsp; 为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    5</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(Locale aLocale) </strong><br>
                    在具有给定语言环境的默认时区内构造一个基于当前时间的 GregorianCalendar。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    6</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(TimeZone zone) </strong><br>
                    在具有默认语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    7</td>
                <td style="width:529px;">
                    <strong>GregorianCalendar(TimeZone zone, Locale aLocale) </strong><br>
                    &nbsp;在具有给定语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。</td>
            </tr>
            </tbody>
        </table>
        <p>这里是GregorianCalendar 类提供的一些有用的方法列表：</p>
        <table class="reference">
            <tbody>
            <tr>
                <td style="width:47px;">
                    <strong>序号</strong></td>
                <td style="width:529px;">
                    <strong>方法和说明</strong></td>
            </tr>
            <tr>
                <td style="width:47px;">
                    1</td>
                <td style="width:529px;">
                    <strong>void add(int field, int amount) </strong><br>
                    根据日历规则，将指定的（有符号的）时间量添加到给定的日历字段中。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    2</td>
                <td style="width:529px;">
                    <strong>protected void computeFields() </strong><br>
                    转换UTC毫秒值为时间域值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    3</td>
                <td style="width:529px;">
                    <strong>protected void computeTime() </strong><br>
                    覆盖Calendar ，转换时间域值为UTC毫秒值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    4</td>
                <td style="width:529px;">
                    <strong>boolean equals(Object obj) </strong><br>
                    比较此 GregorianCalendar 与指定的 Object。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    5</td>
                <td style="width:529px;">
                    <strong>int get(int field) </strong><br>
                    获取指定字段的时间值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    6</td>
                <td style="width:529px;">
                    <strong>int getActualMaximum(int field) </strong><br>
                    返回当前日期，给定字段的最大值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    7</td>
                <td style="width:529px;">
                    <strong>int getActualMinimum(int field) </strong><br>
                    返回当前日期，给定字段的最小值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    8</td>
                <td style="width:529px;">
                    <strong>int getGreatestMinimum(int field) </strong><br>
                    &nbsp;返回此 GregorianCalendar 实例给定日历字段的最高的最小值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    9</td>
                <td style="width:529px;">
                    <strong>Date getGregorianChange() </strong><br>
                    获得格里高利历的更改日期。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    10</td>
                <td style="width:529px;">
                    <strong>int getLeastMaximum(int field) </strong><br>
                    返回此 GregorianCalendar 实例给定日历字段的最低的最大值</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    11</td>
                <td style="width:529px;">
                    <strong>int getMaximum(int field) </strong><br>
                    返回此 GregorianCalendar 实例的给定日历字段的最大值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    12</td>
                <td style="width:529px;">
                    <strong>Date getTime()</strong><br>
                    获取日历当前时间。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    13</td>
                <td style="width:529px;">
                    <strong>long getTimeInMillis() </strong><br>
                    获取用长整型表示的日历的当前时间</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    14</td>
                <td style="width:529px;">
                    <strong>TimeZone getTimeZone() </strong><br>
                    获取时区。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    15</td>
                <td style="width:529px;">
                    <strong>int getMinimum(int field) </strong><br>
                    返回给定字段的最小值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    16</td>
                <td style="width:529px;">
                    <strong>int hashCode() </strong><br>
                    重写hashCode.</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    17</td>
                <td style="width:529px;">
                    <strong>boolean isLeapYear(int year)</strong><br>
                    确定给定的年份是否为闰年。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    18</td>
                <td style="width:529px;">
                    <strong>void roll(int field, boolean up) </strong><br>
                    在给定的时间字段上添加或减去（上/下）单个时间单元，不更改更大的字段。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    19</td>
                <td style="width:529px;">
                    <strong>void set(int field, int value) </strong><br>
                    用给定的值设置时间字段。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    20</td>
                <td style="width:529px;">
                    <strong>void set(int year, int month, int date) </strong><br>
                    设置年、月、日的值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    21</td>
                <td style="width:529px;">
                    <strong>void set(int year, int month, int date, int hour, int minute) </strong><br>
                    设置年、月、日、小时、分钟的值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    22</td>
                <td style="width:529px;">
                    <strong>void set(int year, int month, int date, int hour, int minute, int second) </strong><br>
                    设置年、月、日、小时、分钟、秒的值。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    23</td>
                <td style="width:529px;">
                    <strong>void setGregorianChange(Date date) </strong><br>
                    设置 GregorianCalendar 的更改日期。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    24</td>
                <td style="width:529px;">
                    <strong>void setTime(Date date) </strong><br>
                    用给定的日期设置Calendar的当前时间。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    25</td>
                <td style="width:529px;">
                    <strong>void setTimeInMillis(long millis) </strong><br>
                    用给定的long型毫秒数设置Calendar的当前时间。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    26</td>
                <td style="width:529px;">
                    <strong>void setTimeZone(TimeZone value) </strong><br>
                    用给定时区值设置当前时区。</td>
            </tr>
            <tr>
                <td style="width:47px;">
                    27</td>
                <td style="width:529px;">
                    <strong>String toString() </strong><br>
                    返回代表日历的字符串。</td>
            </tr>
            </tbody>
        </table>
        <h3>实例</h3>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.*;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">GregorianCalendarDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">months</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-quotes">"</span><span class="hl-string">Jan</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Feb</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Mar</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Apr</span><span class="hl-quotes">"</span><span class="hl-code">,
      </span><span class="hl-quotes">"</span><span class="hl-string">May</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Jun</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Jul</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Aug</span><span class="hl-quotes">"</span><span class="hl-code">,
      </span><span class="hl-quotes">"</span><span class="hl-string">Sep</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Oct</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Nov</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Dec</span><span class="hl-quotes">"</span><span class="hl-brackets">}</span><span class="hl-code">;

      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">year</span><span class="hl-code">;
      </span><span class="hl-comment">//</span><span class="hl-comment"> 初始化 Gregorian 日历</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-comment">//</span><span class="hl-comment"> 使用当前时间和日期</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-comment">//</span><span class="hl-comment"> 默认为本地时间和时区</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">GregorianCalendar</span><span class="hl-code"> </span><span class="hl-identifier">gcalendar</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">GregorianCalendar</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-comment">//</span><span class="hl-comment"> 显示当前时间和日期的信息</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Date: </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">months</span><span class="hl-brackets">[</span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">MONTH</span><span class="hl-brackets">)</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">DATE</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">year</span><span class="hl-code"> = </span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">YEAR</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Time: </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">HOUR</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">:</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">MINUTE</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">:</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-identifier">Calendar</span><span class="hl-code">.</span><span class="hl-identifier">SECOND</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-comment">//</span><span class="hl-comment"> 测试当前年份是否为闰年</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">gcalendar</span><span class="hl-code">.</span><span class="hl-identifier">isLeapYear</span><span class="hl-brackets">(</span><span class="hl-identifier">year</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">当前年份是闰年</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">当前年份不是闰年</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>

        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Date</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Apr</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">2009</span><span class="pln">
</span><span class="typ">Time</span><span class="pun">:</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">25</span><span class="pun">:</span><span class="lit">27</span><span class="pln">
</span><span class="pun">当前年份不是闰年</span></pre>
        <p>关于 Calender 类的完整列表，你可以参考标准的 <a target="_blank" href="http://www.runoob.com/manual/jdk1.6/">Java文档</a>。</p>

    </div>
</div>
</body>
</html>
